﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Infrastructures;
using Models;

namespace SponsorshipWeb.personal
{
    public partial class Step2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var user = AuthUtil.GetAuthentication();
                if (user == null)
                    Response.Redirect("../login.html");

                hfUserID.Value = user.UserID.ToString();

                string mode = Request.QueryString["mode"];
                if (mode == "edit")
                {
                    hfMode.Value = mode;
                    BindUI();
                }
            }
        }

        private void BindUI()
        {
            //StudentService service = ServiceFactory.CreateInstance<StudentService>();
            //var student = service.Get<Student>(hfUserID.Value);

            //UtilService utilService = ServiceFactory.CreateInstance<UtilService>();
            //var lst = utilService.ListFileSystem(hfUserID.Value.ToInt());

            //txtID.Text = student.IdentNO;
            //txtAlipay.Text = student.AliPayNO;
            //txtCard.Text = student.BankNO;
            //imgFront.Visible = true;
            //imgBack.Visible = true;
            //imgCover.Visible = true;
            //imgID.Visible = true;
            //imgFront.Src = SetImg(lst, FileType.IDFront);
            //imgBack.Src = SetImg(lst, FileType.IDOpposite);
            //imgCover.Src = SetImg(lst, FileType.StuCover);
            //imgID.Src = SetImg(lst, FileType.StuConetnt);
        }

        private string SetImg(IList<FileSystem> lst, FileType type)
        {
            var img = lst.FirstOrDefault(x => x.Fun == type);
            if (img != null)
                return img.Path;

            return string.Empty;
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            //string strError = string.Empty;

            //if (string.IsNullOrWhiteSpace(hfMode.Value))
            //{
            //    if (fuIDFront.HasFile == false || String.IsNullOrEmpty(fuIDFront.PostedFile.FileName) ||
            //        fuIDBack.HasFile == false || String.IsNullOrEmpty(fuIDBack.PostedFile.FileName) ||
            //        fuStuID.HasFile == false || String.IsNullOrEmpty(fuStuID.PostedFile.FileName) ||
            //        fuStuCover.HasFile == false || String.IsNullOrEmpty(fuStuCover.PostedFile.FileName))
            //    {
            //        strError = "ParseExcel:: No file found, please input a file.";
            //        return;
            //    }
            //}

            //StudentService service = ServiceFactory.CreateInstance<StudentService>();
            //var student = service.Get<Student>(hfUserID.Value);

            //student.IdentNO = txtID.Text;
            //student.AliPayNO = txtAlipay.Text;
            //student.BankNO = txtCard.Text;

            //service.Update(student, student.StudentNO.ToString());

            //IList<FileSystem> lst = null;

            //if (hfMode.Value == "edit" && (fuIDBack.HasFile || fuIDFront.HasFile))
            //{
            //    UtilService utilService = ServiceFactory.CreateInstance<UtilService>();
            //    lst = utilService.ListFileSystem(student.StudentNO);
            //}

            //SaveFile(fuIDFront, FileType.IDFront, lst);
            //SaveFile(fuIDBack, FileType.IDOpposite, lst);
            //SaveFile(fuStuID, FileType.StuConetnt, lst);
            //SaveFile(fuStuCover, FileType.StuCover, lst);

            //Response.Redirect("info.html");
        }

        private void SaveFile(FileUpload fu, FileType type, IList<FileSystem> lst)
        {
            //if (!fu.HasFile)
            //    return;

            //string fileName = fu.FileName;
            //string fileExt = Path.GetExtension(fileName);
            //string strDir = SystemConfig.Get("UploadPath") + "Student\\" + hfUserID.Value + "\\";

            //if (!Directory.Exists(strDir))
            //    Directory.CreateDirectory(strDir);

            //string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
            //fu.SaveAs(strDir + strSysName);

            //FileSystem fs = null;

            //if (lst != null && lst.Count > 0)
            //    fs = lst.FirstOrDefault(x => x.Fun == type);
            //else
            //    fs = new FileSystem();

            //fs.Fun = type;
            //fs.TypeName = APPRole.Student;
            //fs.Belong = hfUserID.Value.ToInt();
            //fs.NewName = strSysName;
            //fs.OldName = fileName;
            //fs.Path = SystemConfig.Get("UploadHttpPath") + "Student/" + hfUserID.Value + "/" + strSysName;
            //fs.Status = CommonStatus.Active;
            //fs.InstanceID = SystemConfig.Get("InstanceID");
            //fs.CreateUser = hfUserID.Value.ToInt();

            //UtilService service = FactoryCreator.Create<UtilService>();

            //if (lst == null || lst.Count == 0)
            //    service.CreateFile(fs);
            //else
            //    service.UpdateFile(fs);
        }
    }
}